package com.bishe.controller;

import com.bishe.pojo.Org;
import com.bishe.serivce.IOrgService;
import com.bishe.utils.EcoBootException;
import com.bishe.utils.JobUtil;
import com.bishe.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 部门控制层
 * @date 2022/03/20 42:08:37
 */
@RestController
@RequestMapping("/org")
@Api(tags = "部门控制器")
@CrossOrigin
public class OrgController {
    @Autowired
    private IOrgService orgService;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @ApiOperation("添加和修改部门")
    @PostMapping("/addOrUpdate")
    public Result addOrUpdate(@RequestBody Org org,HttpServletRequest request){
        //检查admin登录
        JobUtil.checkAdminLogin(request,jdbcTemplate);
        if (org == null){
            throw new EcoBootException("org 为空！");
        }
        orgService.addOrUpdate(org);

        return Result.ok();
    }



    @ApiOperation("部门列表")
    @PostMapping("/list")
    public Result list(){

        List<Org> orgs =  orgService.list();

        return Result.ok(orgs);
    }


    @ApiOperation("删除部门")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id")String id,HttpServletRequest request){

        //检查admin登录
        JobUtil.checkAdminLogin(request,jdbcTemplate);
        orgService.delete(id);

        return Result.ok();
    }
}
